<?php
	/**
	 * Created by PhpStorm.
	 * User: mybook-lhp
	 * Date: 18/7/6
	 * Time: 下午3:31
	 */

	namespace app\payment\logic;

	use EasyWeChat\Factory;
	use Omnipay\Omnipay;
	use think\Log;

	class Easypay extends PayBase
	{
		/**
		 * easywechat 版支付
		 * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
		 */
		static public function eminiapp($pay_data)
		{
			$res = static::checkData($pay_data);
			if ($res !== 'ok')
			{
				return $res;
			}
			//获取配置
			$config = self::getConfig($pay_data['pay_type']);
			if (!$config)
			{
				return '微信配置不存在！';
			}
			$config['app_id'] = $config['appid'];
			$config['key'] = $config['mch_key'];
			unset($config['appid']);
			unset($config['secret']);
			$app = Factory::payment($config);

			$order = [
				'body'             => $pay_data['pay_name'],
				'out_trade_no'     => $pay_data['pay_sn'],
				'total_fee'        => floor($pay_data['amoun'] * 100), //=0.01
				//				'total_fee'        => 1, //=0.01
				'spbill_create_ip' => gethostbyname($_SERVER['SERVER_NAME']),
				'fee_type'         => 'CNY',
				'trade_type'       => 'JSAPI',
				'openid'           => $pay_data['open_id'],
			];

			if (isset($pay_data['notify_url']) && $pay_data['notify_url'] != '')
			{
				$order['notify_url'] = request()->domain() . $pay_data['notify_url'];
			} else
			{
				$order['notify_url'] = request()->domain() . '/api.php/paygateway/callback/wechatpay';
			}
			return $result = $app->order->unify($order);

			return [
				'$config' => $config,
				'$order'  => $order,
				'$result' => $result,
			];

		}


		public function go($pay_data)
		{
			//获取用户的openid
			$config = self::getConfig($pay_data['pay_type']);
			$wehchtData = [
				'app_id'        => 'wxd81325bbc4f971e0',//开发者ID  wxd81325bb4f971e0
				'secret'        => 'dce51e04dfa64bedc752697d447bde26',//1开发者密钥 53d899fdcf6eb1397c5cf4003c9191b2  dce51e04dfa64bedc752697d447bde26
				'token'         => 'sdxvTQLlNhiEk9d8PsYzUpzToqUWMEoo',//53d899fdcf6eb1397c5cf4003c9191b2
				'aes_key'       => 'uUH1jPNrFe0Anj4CTtY6SymedEfZqBf7K1r9kGrAHZ2',//uUH1jPNrFe0Anj4CTtY6SymedEfZqBf7K1r9kGrAHZ2

				//商户配置
				'mch_id'        => '1498316492',//1 1498316492  1315557501 19A02C5F04DC84E74092D90FC70F059B  1499240212   商户ID1315557501
				'key'           => '5Rduwfp6yYL14dNUBUQkVYhUyJ5Fx0Wz',//1 5Rduwfp6yYL14dNUBUQkVYhUyJ5Fx0Wz
				'notify_url'    => '/wechat/payment/collback',    // 你也可以在下单时单独设置来想覆盖它

				// 如需使用敏感接口（如退款、发送红包等）需要配置 API 证书路径(登录商户平台下载 API 证书)
				'cert_path'     => \think\facade\Env::get('app_path') . 'wechat/cert/apiclient_cert.pem', // XXX: 绝对路径！！！！
				'key_path'      => \think\facade\Env::get('app_path') . 'wechat/cert/apiclient_key.pem',      // XXX: 绝对路径！！！！
				'sandbox'       => false, // 设置为 false 或注释则关闭沙箱模式
				// 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
				'response_type' => 'array',
			];


			$app = Factory::payment($config);
			$status = $app->transfer->toBalance([
				'partner_trade_no' => rand('11111', '99999999') . time(), // 商户订单号，需保持唯一性(只能是字母或者数字，不能包含有符号)
				'openid'           => 'oNh2Xs4vihRSdGXbe3SjaPVd-Me0',//;//$openid[0]['openid'],//'okYEy0oR02uxg-XQjoUt9Mpe7-UM',
				'check_name'       => 'NO_CHECK', // NO_CHECK：不校验真实姓名, FORCE_CHECK：强校验真实姓名
				// 're_user_name' => $name['truename'], // 如果 check_name 设置为FORCE_CHECK，则必填用户真实姓名
				'amount'           => $this->request->get('m'), // 企业付款金额，单位为分
				'desc'             => '...', // 企业付款操作说明信
			]);
			//        //判断是否打款成功
			if ($status['result_code'] == "SUCCESS")
			{

				return ['code' => 200, 'mgs' => '打款成功', $status];
			} else
			{
				return ['code' => 404, 'mgs' => '打款失败', $status];
			}

		}

	}